package com.msb.mall.ware.controller;

import com.msb.common.utils.PageUtils;
import com.msb.common.vo.CommonPageReq;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.entity.ware.PurchaseEntity;
import com.msb.mall.core.vo.ware.MergeVO;
import com.msb.mall.core.vo.ware.PurchaseDoneVO;
import com.msb.mall.core.vo.ware.PurchaseItemDoneVO;
import com.msb.mall.ware.service.PurchaseService;
import com.sun.xml.internal.ws.handler.HandlerException;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 采购信息
 *
 * @author leixu
 * @email xuleiwolf@163.com
 * @date 2024-04-19 20:31:49
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 分页
     */
    @PostMapping("/page")
    public CommonResult page(@RequestBody CommonPageReq<Map<String, Object>> params){
        PageUtils page = purchaseService.queryPageCondition(params);

        return CommonResult.success(page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public CommonResult list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return CommonResult.success(null, page);
    }

    /**
     * 列表
     */
    @GetMapping("/unreceive/list")
    //@RequiresPermissions("ware:purchase:list")
    public CommonResult listUnReceive(@RequestParam Map<String, Object> params){
        return CommonResult.success(purchaseService.queryListUnReceive(params));
    }

    /**
     * 合并采购单前数据校验
     * @param mergeVO
     * @return
     * @throws Exception
     */
    @PostMapping("/mergeCheck")
    //@RequiresPermissions("ware:purchase:list")
    public CommonResult mergeCheck(@RequestBody MergeVO mergeVO) throws Exception{
        return purchaseService.mergeCheck(mergeVO);
    }

    /**
     * 合并采购单
     */
    @PostMapping("/merge")
    //@RequiresPermissions("ware:purchase:list")
    public CommonResult merge(@RequestBody MergeVO mergeVO) throws Exception{
        return purchaseService.merge(mergeVO);
    }

    /**
     * 领取采购单
     * [2,3,4]
     * @return
     */
    @PostMapping("/receive")
    public CommonResult receive(@RequestBody List<Long> ids){
        purchaseService.received(ids);
        return CommonResult.success();
    }

    /**
     * 完成采购
     * {
     *     id:1,// 采购单
     *     items:[
     *      {itemId:1,status:4,reason:""}
     *      ,{itemId:2,status:4,reason:""}
     *     ]//采购项
     * }
     */
    @PostMapping("/done")
    public CommonResult done(@RequestBody PurchaseDoneVO vo){
        purchaseService.done(vo);
        return CommonResult.success();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public CommonResult info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return CommonResult.success(purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public CommonResult save(@RequestBody PurchaseEntity purchase){
		purchaseService.save(purchase);

        return CommonResult.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public CommonResult update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return CommonResult.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public CommonResult delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return CommonResult.success();
    }

}
